<?
#####################################################
#  COPYRIGHT NOTICE
#####################################################
# In the following statements, the term "this 
# program" refers to all files associated with the
# execution and distribution of Adtrackz.
#
# This Program is Copyright 2004 Jonah Klimack.
# All Rights Reserved. 
#
# Selling the code for this program, modifying 
# and/or redistributing the code for this program
# in part or in whole over the Internet or in any 
# other medium is expressly forbidden. Violators
# will be prosecuted to the fullest extent of the law
# Copyright and header information may not be
# modified.
#
# This program is distributed "as is" and without 
# warranty of any kind, either express or implied.
# In no event shall the liability of Jonah Klimack 
# for any damages, losses and/or causes of 
# action exceed the total amount paid by the 
# user for this software.		
#####################################################
# DO NOT MODIFY ANYTHING IN THIS FILE
#####################################################


require "header.php";
require "submenu.php";

//campaign info
$campaign = mysql_fetch_row(mysql_query("select * from adtrackz_campaigns where ID='$cID'"));
$sub = mysql_fetch_row(mysql_query("select * from adtrackz_subcampaigns where ID='$sID'"));
$group = mysql_fetch_row(mysql_query("select groupname from adtrackz_groups where ID='$campaign[1]'"));

//GET DATA FOR VARIABLES
$rawclickdata = mysql_fetch_row(mysql_query("select count(ID) from adtrackz_clicks where campaignID='$cID' && subID='$sID'"));
$clickdata = mysql_fetch_row(mysql_query("select count(ID) from adtrackz_clicks where campaignID='$cID' && subID='$sID' && uniqueflag = 1"));

$salesdata = mysql_fetch_row(mysql_query("select count(ID) from adtrackz_sales where campaignID='$cID' && subID='$sID'"));
$actionsdata = mysql_fetch_row(mysql_query("select count(ID) from adtrackz_actions where campaignID='$cID' && subID='$sID'"));
$revdata = mysql_fetch_row(mysql_query("select sum(revenue) from adtrackz_sales where campaignID='$cID' && subID='$sID'"));
$avgrevdata = mysql_fetch_row(mysql_query("select avg(revenue) from adtrackz_sales where campaignID='$cID' && subID='$sID'"));
$fclickdata = mysql_fetch_row(mysql_query("select min(timest) from adtrackz_clicks where campaignID='$cID' && subID='$sID'"));
$lclickdata = mysql_fetch_row(mysql_query("select max(timest) from adtrackz_clicks where campaignID='$cID' && subID='$sID'"));

//convert to "nice time"
if ($fclickdata[0])
	$firstclick = date("D M j G:i:s T Y", $fclickdata[0]);
else
	$firstclick = "n/a";
if ($lclickdata[0])
	$lastclick = date("D M j G:i:s T Y", $lclickdata[0]);
else
	$lastclick = "n/a";

$start_date = strtotime($campaign[5]);
$start_date = date("D M j, Y", $start_date);
$end_date = strtotime($campaign[6]);
$end_date = date("D M j, Y", $end_date);


//CALCULATE COST
if (!$sID) //no sub
{
	if ($campaign[11])
	{
		//true cost
		$cpc_data = mysql_fetch_row(mysql_query("select sum(cpc) from adtrackz_clicks where campaignID='$cID' && subID = 0 && uniqueflag = 1"));
		$cost = $cpc_data[0];
	}
	elseif ($campaign[9] && $campaign[10]) 
	{
		//cost = from start date to now 
		$now = time();
		$startdate = strtotime($campaign[5]);

		//cycles so far, expressed in days
		$cycles = (($now - $startdate) / 86400) / $campaign[9]; 
		if ($cycles > $campaign[10]) //user defines how many cycles
			$cycles = $campaign[10];
		$cost = $cycles * $campaign[4]; //abs cost is # of cycles * specified campaign cost				
	}
	else //normal cost
		$cost = $campaign[4];
}
else
{
	if ($sub[6])
	{
		//true cost
		$cpc_data = mysql_fetch_row(mysql_query("select sum(cpc) from adtrackz_clicks where campaignID='$cID' && subID = '$sID' && uniqueflag = 1"));
		//cost is sum of all clicks in this period, 
		//remember that in go.php if click doesn't have cpc set, then default cpc from campaign is used
		//therefore, cpc should always have a value
		$cost = $cpc_data[0];
	}
	else //normal cost
		$cost = $sub[4];
}

//SET VARIABLES
$clicks = $clickdata[0];
$rawclicks = $rawclickdata[0];
$sales = $salesdata[0];
$actions = $actionsdata[0];
$cpc = (!$clicks ? 0 : $cost / $clicks);
$cpa = (!$actions ? 0 : $cost / $actions);
$cps =  (!$sales ? 0 : $cost / $sales);
$cta = (!$clicks ? 0 : ($actions / $clicks) * 100);
$cts = (!$clicks ? 0 : ($sales / $clicks) * 100);
$avgrev = $avgrevdata[0];
$rev = (!$revdata[0] ? 0 : $revdata[0]);
$profit = $rev - $cost;
$roi = (!$cost ? 0 : $profit / $cost * 100);

$gppc = (!$clicks ? 0 : $rev / $clicks);
$nppc = (!$clicks ? 0 : $profit / $clicks);
$gpps = (!$sales ? 0 : $rev / $sales);
$npps = (!$sales ? 0 : $profit / $sales);

//number_format 
$rawclicks = number_format($rawclicks, 0);
$clicks = number_format($clicks, 0);
$actions = number_format($actions, 0);
$sales = number_format($sales, 0);
$cta = number_format($cta, 2);
$cts = number_format($cts, 2);
$cpc = number_format($cpc, 2);
$cpa = number_format($cpa, 2);
$cps = number_format($cps, 2);
$rev = number_format($rev, 2);
$cost = number_format($cost, 2);
$profit = number_format($profit, 2);
$gppc = number_format($gppc, 2);
$nppc = number_format($nppc, 2);
$gpps = number_format($gpps, 2);
$npps = number_format($npps, 2);
$avgpps = number_format($avgrev, 2);
$roi = number_format($roi, 2);

?>

<table width="100%">
<tr>
	<td>
		<p class="small" align="left">Campaigns > Detailed Stats > Summary</p>
	</td>
	<td>
		<? load_submenu($cID,$campaign[2]) ?>
	</td>
</tr>
</table>


<p class="heading">
Statistic Summary for <?=ucfirst($campaign[2])?>
</p>

<p>
Here you can view a summary of all statistics for <?=ucfirst($campaign[2])?>.  Use the pulldown menu to switch between campaigns and subcampaigns. 
</p>


<Br><br>

<center>
<p>
<b>
<?
if ($sID)
	echo "Displaying Statistics for Subcampaign: $sub[2]";
else
{
	echo "Displaying Statistics for Campaign:";
	echo ucfirst($campaign[2]);
}
?>
</b>
</p>

<p>

<form action="dstats-summary.php" method="post">
<select name="sID">
<?
	if (!$sID)
		echo "<option value=\"\" selected>Campaign: $campaign[2]</option>";
	else
		echo "<option value=\"\">Campaign: $campaign[2]</option>";	

	//get all the subcampaigns, display them, sorted
	$allsubs = mysql_query("select * from adtrackz_subcampaigns where campaignID='$cID' order by adcode asc");	
	while ($allsub = mysql_fetch_row($allsubs))
	{
		if ($sID == $allsub[0])
			echo "<option value=\"$allsub[0]\" selected>subcampaign: $allsub[2]</option>";
		else
			echo "<option value=\"$allsub[0]\">subcampaign: $allsub[2]</option>";
	}
?>
</select>
<input type="hidden" name="allstats" value="yes">
<input type="hidden" name="cID" value="<?=$cID?>">
<input type="submit" name="submit" value="View Summary"  >
</form>
</p>

<table width=500 align=center>
<tr>
	<td width=200 align=center>
		<table border=1 width=100% cellspacing=0 cellpadding=5 align=center bordercolor=#0F87BE>
		<tr>
			<td class=tbhead2>Traffic</td>
		</tr>
		<tr>
			<td>
				<table width=100%>
				<tr>
					<td>Raw Clicks:</td>
					<td><?=$rawclicks?></td>
				</tr>
				<tr>
					<td>Clicks:</td>
					<td><?=$clicks?></td>
				</tr>
				<tr>
					<td>Cost Per Click:
					<td>$<?=$cpc?></td>
				</tr>
				<tr>
					<td>Gross Profit/Click:
					<td>$<?=$gppc?></td>
				</tr>
				<tr>
					<td>Net Profit/Click:
					<td>$<?=$nppc?></td>
				</tr>
				</table>
			</td>
		</tr>
		</table>
	</td>
	<td rowspan="4" valign=top width=300 align=center>
		<table border=1 width=100% cellspacing=0 cellpadding=5 align=center bordercolor=#0F87BE>
		<tr>
			<td class=tbhead2>
			<?
			 if (!$sID)
				echo "Campaign Information";
			else
				echo "SubCampaign Information";
			?>
			</td>
		</tr>
		<tr>
			<td>
				<table width=100%>
				<tr>
					<td>Adcode:</td>
					<td>
					<?
					 if (!$sID)
						echo $campaign[2];
					else
						echo $sub[2];
					?>
					</td>
				</tr>
				<tr>
					<td>Group:
					<td>
						<?
						if (!$group[0])
							echo "default";
						else
							echo $group[0];
						?>
					</td>
				</tr>
				<tr>
					<td>Purpose:
					<td>
						<?
						if ($campaign[8] == "all")
							echo "Clicks, Actions & Sales";
						elseif ($campaign[8] == "clicks")
							echo "Clicks Only";
						elseif ($campaign[8] == "actions")
							echo "Clicks & Actions";
						elseif ($campaign[8] == "sales")
							echo "Clicks & Sales";
						elseif ($campaign[8] == "affiliate")
							echo "Clicks, Sales, Actions on Affiliate Websites";
						?>
					</td>
				</tr>
				<tr>
					<td>Destination Url:</td>
					<td>
					<?
					if (!$sID)
						echo "<textarea cols=30 rows=2>$campaign[3]</textarea>";
					else
						echo "<textarea cols=30 rows=2>$sub[3]</textarea>";
					?>
					</td>
				</tr>
				<tr>
					<td>
					<?
					if ($campaign[11])
						echo "Cost Per Click:";
					else
						echo "Cost:";
					?>
					</td><td>
					$<?=$campaign[4]?>
					</td>
				</tr>
				<tr>
					<td>Cost Type:</td>
					<td>
						<?
						if (!$sID)
							if ($campaign[9] && $campaign[10])
								echo "Recurring every $campaign[9] days, $campaign[10] times.";
							elseif ($campaign[11])
								echo "Pay Per Click";
							else
								echo "One-Time";
						else
							if ($sub[6])
								echo "Pay Per Click";
							else
								echo "One-Time";
						?>
					</td>
				</tr>
				<tr>
					<td>Start Date:</td>
					<td><?=$start_date?></td>
				</tr>
				<tr>
					<td>End Date:</td>
					<td><?=$end_date?></td>
				</tr>
				<tr>
					<td>First Click:</td>
					<td><?=$firstclick?></td>
				</tr>
				<tr>
					<td>Last Click:</td>
					<td><?=$lastclick?></td>
				</tr>
				<tr>
					<td>Description:</td>
					<td>
					<?
					if (!$sID)
						echo $campaign[7];
					else
						echo $sub[5];
					?>
					</td>
				</tr>
				<tr>
					<td>Tracking Link:</td>
					<td>
					<?
						if ($sID)
							$tr_string= "?c=$campaign[2]&s=$sub[2]";
						else
							$tr_string=	"?c=$campaign[2]";
						echo "<textarea cols=30 rows=2>http://$domain/$udirectory/go.php$tr_string</textarea>";
					?>
					</td>
				</tr>
				<tr>
					<td>Clickable Tracking Url:</td>
					<td>
					<?
						if ($sID)
							$tr_string= "?c=$campaign[2]&s=$sub[2]";
						else
							$tr_string=	"?c=$campaign[2]";
						echo "<a href=\"http://$domain/$udirectory/go.php$tr_string\">http://$domain/$udirectory/go.php$tr_string</a>";
					?>
					</td>
				</tr>
				</table>
			</td>
		</tr>
		</table>
	</td>
</tr>
<tr>
	<td width=200 align=center>
		<table border=1 width=100% cellspacing=0 cellpadding=5 align=center bordercolor=#0F87BE>
		<tr>
			<td class=tbhead2>Actions</td>
		</tr>
		<tr>
			<td>
				<table width=100%>
				<tr>
					<td>Actions:</td>
					<td><?=$actions?></td>
				</tr>
				<tr>
					<td>Cost Per Action:
					<td>$<?=$cpa?></td>
				</tr>
				<tr>
					<td>Conversion:</td>
					<td><?=$cta?>%</td>
				</tr>
				</table>
			</td>
		</tr>
		</table>
	</td>
</tr>
<tr>
	<td width=200 align=center>
		<table border=1 width=100% cellspacing=0 cellpadding=5 align=center bordercolor=#0F87BE>
		<tr>
			<td class=tbhead2>Sales</td>
		</tr>
		<tr>
			<td>
				<table width=100%>
				<tr>
					<td>Sales:</td>
					<td><?=$sales?></td>
				</tr>
				<tr>
					<td>Cost Per Sale:
					<td>$<?=$cps?></td>
				</tr>
				<tr>
					<td>Conversion</td>
					<td><?=$cts?>%</td>
				</tr>
				<tr>
					<td>Avg Profit/Sale:
					<td>$<?=$avgpps?></td>
				</tr
				<tr>
					<td>Gross Profit/Sale:
					<td>$<?=$gpps?></td>
				</tr>
				<tr>
					<td>Net Profit/Sale:
					<td>$<?=$npps?></td>
				</tr>
				</table>
			</td>
		</tr>
		</table>
	</td>
</tr>
<tr>
	<td width=200 align=center>
		<table border=1 width=100% cellspacing=0 cellpadding=5 align=center bordercolor=#0F87BE>
		<tr>
			<td class=tbhead2>Totals</td>
		</tr>
		<tr>
			<td>
				<table width=100%>
				<tr>
					<td>Cost</td>
					<td>$<?=$cost?></td>
				</tr>
				<tr>
					<td>Revenue:</td>
					<td>$<?=$rev?></td>
				</tr>
				<tr>
					<td>Profit</td>
					<td>$<?=$profit?></td>
				</tr>
				<tr>
					<td>R.O.I.</td>
					<td>
					<?
					if ($roi != "n/a")
						echo "$roi%";
					else
						echo $roi;
					?>
					</td>
				</tr>
				</table>
			</td>
		</tr>
		</table>
	</td>
</tr>
</table>

<?

require "footer.php";

?>